<template>
  <div class="choiceGoods">
    <el-dialog
      :title="dialogVisibleTile"
      :visible.sync="dialogVisible"
      width="780px"
      :destroy-on-close="true"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        :labelWidth="labelWidth"
        class="demo-ruleForm"
      >
        <el-row :gutter="10">
          <el-col :xs="24" :sm="24">
            <el-form-item label="商品名称：" prop="goodsName">
              <el-input
                v-model="ruleForm.goodsName"
                placeholder="请输入商品名称"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="onSearch"
                ></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" style="text-align: center">
            <el-transfer
              style="text-align: left; display: inline-block"
              v-model="ruleForm.chioseVal"
              :titles="['全选商品', '已选商品']"
              :button-texts="['到左边', '到右边']"
              @change="chioseGoods"
              :format="{
                noChecked: '${total}',
                hasChecked: '${checked}/${total}',
              }"
              :data="dataList"
            ></el-transfer>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleSure()" :loading="loading"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { pageByShopId } from "@/api/marketing/activeList.js";

export default {
  data() {
    return {
      dialogVisible: false,
      dialogVisibleTile: "",
      loading: false,
      labelWidth: "120px",
      rules: {
        lmmc: [
          {
            required: true,
            message: "请输入分组名称，不超过15个字",
            trigger: "blur",
          },
        ],
      },
      pid: [],
      ruleForm: {
        chioseVal: [],
        chioseGoods: [],
      },
      dataVal: [],
      categoryIdList: [],
      dataList: [],
      shopId: "",
    };
  },
  methods: {
    getList(shopId) {
      if (shopId) {
        this.shopId = shopId;
      }
      console.log("shopId", shopId);
      console.log(this.$store.getters.userInfo.deptId);
      
      pageByShopId({
        shopId: this.shopId,
        goodsName: "",
        deptId: this.$store.getters.userInfo.deptId,
      }).then((val) => {
        this.dataVal = val.data.data;
      });

      pageByShopId({
        shopId: this.shopId,
        goodsName: this.ruleForm.goodsName,
        deptId: this.$store.getters.userInfo.deptId,
      }).then((val) => {
        let data = val.data.data;
        this.dataList = data.map((item) => {
          return {
            key: item.id,
            label: item.goodsName + "-" + item.shopName,
          };
        });
      });
    },
    handleSure() {
      console.log(this.ruleForm);
      this.$emit("trigger", this.ruleForm);
      this.handleClose();
    },
    handleClose() {
      this.dialogVisible = false;
    },
    chioseGoods(e) {
      this.ruleForm.chioseGoods = [];
      console.log(e);
      e.forEach((item) => {
        this.dataVal.forEach((items) => {
          if (items.id == item) {
            this.ruleForm.chioseGoods.push(items);
          }
        });
      });
    },
    onSearch() {
      this.getList();
    },
    categoryIdChange(e) {
      if (e.length > 1) {
        this.ruleForm.categoryId = e[1];
      } else {
        this.ruleForm.categoryId = e[0];
      }
      this.getList({ categoryId: this.ruleForm.categoryId });
    },
  },
};
</script>

<style>
</style>